<?php

use think\migration\Migrator;
use think\migration\db\Column;

class InitAuthTables extends Migrator
{
    /**
     * Change Method.
     *
     * Write your reversible migrations using this method.
     *
     * More information on writing migrations is available here:
     * http://docs.phinx.org/en/latest/migrations.html#the-abstractmigration-class
     *
     * The following commands can be used in this method and Phinx will
     * automatically reverse them when rolling back:
     *
     *    createTable
     *    renameTable
     *    addColumn
     *    renameColumn
     *    addIndex
     *    addForeignKey
     *
     * Remember to call "create()" or "update()" and NOT "save()" when working
     * with the Table class.
     */
    public function change()
    {
        // 用户表
        $this->table('auth_users')
            ->addColumn('username', 'string', ['limit' => 50, 'null' => false, 'comment' => '用户名'])
            ->addColumn('password', 'string', ['limit' => 255, 'null' => false, 'comment' => '密码'])
            ->addColumn('nickname', 'string', ['limit' => 50, 'null' => true, 'comment' => '昵称'])
            ->addColumn('avatar', 'string', ['limit' => 255, 'null' => true, 'comment' => '头像'])
            ->addColumn('status', 'integer', ['limit' => 1, 'default' => 1, 'comment' => '状态：0禁用 1启用'])
            ->addColumn('create_time', 'integer', ['null' => false, 'comment' => '创建时间'])
            ->addColumn('update_time', 'integer', ['null' => false, 'comment' => '更新时间'])
            ->addIndex(['username'], ['unique' => true])
            ->create();

        // 第三方登录表
        $this->table('auth_identities')
            ->addColumn('user_id', 'integer', ['null' => false, 'comment' => '用户ID'])
            ->addColumn('type', 'string', ['limit' => 20, 'null' => false, 'comment' => '登录类型：wechat/douyin'])
            ->addColumn('openid', 'string', ['limit' => 100, 'null' => false, 'comment' => '第三方唯一标识'])
            ->addColumn('unionid', 'string', ['limit' => 100, 'null' => true, 'comment' => '第三方联合ID'])
            ->addColumn('extra', 'text', ['null' => true, 'comment' => '额外信息'])
            ->addColumn('create_time', 'integer', ['null' => false, 'comment' => '创建时间'])
            ->addColumn('update_time', 'integer', ['null' => false, 'comment' => '更新时间'])
            ->addIndex(['type', 'openid'], ['unique' => true])
            ->addIndex(['user_id'])
            ->create();

        // Token黑名单表
        $this->table('auth_token_blacklist')
            ->addColumn('token', 'string', ['limit' => 255, 'null' => false, 'comment' => 'JWT Token'])
            ->addColumn('create_time', 'integer', ['null' => false, 'comment' => '创建时间'])
            ->addIndex(['token'], ['unique' => true])
            ->create();
    }
}
